<template>
	<div class="myTeamList">
		<div class="memberTitle">我的团队列表</div>
		<div class="menberContent">
			<div class="menberConTitle">
				<el-button round size="mini" type="primary" :disabled="pageData.length > 0?false:true" @click="toTop">置顶</el-button>
				<el-button round size="mini" type="primary" :disabled="pageData.length > 0?false:true" @click="settingZ">设为主团队</el-button>
				<el-button round size="mini" type="primary" :disabled="pageData.length > 0?false:true" @click="chitchat">聊天</el-button>
				<el-button round size="mini" type="danger" :disabled="pageData.length > 0?false:true" @click="exitTeam">退出团队</el-button>
			</div>
			<el-table ref="multipleTable" :data="pageData" style="width: 100%">
				<el-table-column label="" width="55">
					<template slot-scope="scope">
						<el-checkbox v-model="scope.row.isSelect" :disabled="scope.row.isSelect && scope.row.principal == 1?true:false"
						 @change="tablecheckbox(scope.row)"></el-checkbox>
					</template>
				</el-table-column>
				<el-table-column label="团队名称">
					<template slot-scope="scope">
						<span class="ztd" v-if="scope.row.principal == 1">（主）</span>{{ scope.row.labelName }}
					</template>
				</el-table-column>
				<el-table-column prop="platformNumber" label="平台编号"></el-table-column>
				<el-table-column prop="checkState" label="认证情况">
					<template slot-scope="scope">
						{{ scope.row.checkState=='SUCCESS'?'已认证':'未认证' }}
					</template>
				</el-table-column>
				<el-table-column prop="departmentName" label="所在部门">
					<template slot-scope="scope">
						{{scope.row.departmentName?scope.row.departmentName:'暂无部门'}}
					</template>
				</el-table-column>
				<el-table-column prop="dutiesName" label="管理角色"></el-table-column>
				<el-table-column prop="administrator" label="管理范围"></el-table-column>
			</el-table>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				userId: '',
				pageData: [],
				tableSlection: new Map(),
				selectRowId: ''
			}
		},
		mounted() {
			this.$store.commit('set_teamNavIndex', '')
			this.userId = this.$store.state.user.userInfo.id
			this.getCompanyList()
		},
		methods: {
			getCompanyList() {
				// 获取用户所有公司列表
				this.$request.postForm(this.$api.getCompanyByUser, {
					userId: this.userId
				}).then(res => {
					if (res.code == 200) {
						// this.tableSlection
						this.pageData = []
						this.tableSlection.clear()
						res.data.forEach((v) => {
							if (v.principal == 1) {
								v.isSelect = true
							} else {
								v.isSelect = false
							}
							this.tableSlection.set(v.id, v)
						})
						this.$nextTick(() => {
							this.pageData = [...this.tableSlection.values()]
							this.selectRowId = this.pageData[0]
							this.$store.commit('set_companyList', this.pageData)
						})
					}
				})
			},
			tablecheckbox(row) {
				this.selectRowId = row
				this.tableSlection.forEach((v, k) => {
					if (v.principal != 1 && v.id != row.id) {
						v.isSelect = false
					} else {
						v.isSelect = true
					}
				})
			},
			toTop() {
				// 置顶
				if(!this.selectRowId&&!this.selectRowId.id){
					return
				}
				this.$request.postForm(this.$api.updateCompanyTopByUser, {
					userId: this.userId,
					companyId: this.selectRowId.id,
					topMark: 1
				}).then(res => {
					if (res.code == 200) {
						this.$message.success('设置成功')
						this.getCompanyList()
					}
				})
			},
			settingZ() {
				// 设置主团队
				if(!this.selectRowId&&!this.selectRowId.id){
					return
				}
				this.$confirm('确定要设置当前团队为主团队吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$request.postForm(this.$api.changeOfMainCompany, {
						userId: this.userId,
						companyId: this.selectRowId.id,
					}).then(res => {
						if (res.code == 200) {
							this.$message.success('设置成功')
							this.getCompanyList()
						}
					})
				})
			},
			exitTeam() {
				// 退出团队
				if(!this.selectRowId&&!this.selectRowId.id){
					return
				}
				let outId = ''
				if (this.selectRowId) {
					outId = this.selectRowId.id
				} else {
					this.pageData.forEach((v) => {
						if (v.principal == 1) {
							outId = v.id
						}
					})
				}
				this.$confirm('确定要退出该团队吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$request.postForm(this.$api.outCompany, {
						userId: this.userId,
						companyId: outId,
					}).then(res => {
						if (res.code == 200) {
							this.$message.success('已退出该团队')
							this.getCompanyList()
						}
					})
				})
			},
			chitchat(){
				// 聊天
				sessionStorage.setItem('applyUserId', this.selectRowId.id)
				sessionStorage.setItem('applyChatType', '1')
				const url = `/addresslist/interior/message?t=${new Date().getTime()}`
				this.$router.push(url)
			}
		}
	}
</script>

<style scoped="scoped">
	.memberTitle {
		color: #333333;
		font-size: 20px;
		padding: 20px;
	}

	.menberContent {
		padding: 0 20px;
		padding-bottom: 20px;
	}

	.menberConTitle {
		width: 100%;
		background: #F3F3F3;
		text-align: left;
		padding: 5px 20px;
		margin-bottom: 20px;
	}

	.ztd {
		color: red;
	}
</style>
